"""
Case Type   : 资源池化
Case Name   : DFX 重启主机操作
Create At   : 2024.01
Owner       : chen
Description :
    1、查询集群初始状态：cm_ctl query -Cvidp
    2、主机备机：cm_ctl stop -n 2 -I 6002
    3、主机备机：cm_ctl start -n 2 -I 6002
Expect      :
    1、集群初始状态正常
    2、停止主机成功
    3、主机备机成功
"""

import os
import unittest
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.com = Common()
        self.pri_sh = CommonSH('PrimaryDbUser')
    
    def test_shared_storage(self):
        text = '-----step1:查询集群初始状态: cm_ctl query -Cvidp;' \
            ' expect: 集群初始状态正常-----'
        self.log.info(text)
        status = self.pri_sh.exec_cm_ctl('query', '-Cvidp')
        self.log.info(status)
        self.assertIn("cluster_state   : Normal", status)

        text = '-----step2:停止备机：cm_ctl stop -n 1 -I 6001 ' \
            'expect: 停止备机成功-----'
        res = self.pri_sh.exec_cm_ctl('stop -n 1 -I 6001')
        self.log.info(res)
        self.assertIn(self.constant.cm_stop_resource_msg, res)

        text = '-----step3:停止备机：cm_ctl start -n 1 -I 6001 ' \
            'expect: 停止备机成功-----'
        res = self.pri_sh.exec_cm_ctl('start -n 1 -I 6001')
        self.log.info(res)
        self.assertIn(self.constant.cm_start_resource_success_msg, res)
    
    def tearDown(self):
        self.log.info('-----step4:恢复环境-----')
        self.log.info('恢复集群')
        res = self.pri_sh.restart_db_cluster()
        self.log.info(res)
        status = self.pri_sh.exec_cm_ctl('switchover', '-a')
        self.log.info(status)
        self.assertIn(self.constant.cm_switchover_success_msg, status)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
